const ws = require('ws')

  ; ((ws) => {
    const app = new ws.Server({
      port: 3000,
      host: '192.168.3.10'
    })

    const init = () => {
      bindEvent()
    }

    function bindEvent() {
      app.on('open', hdOpen)
      app.on('close', hdClose)
      app.on('error', hdError)
      app.on('connection', hdConnection)
    }

    function hdOpen() {
      console.log('WebSocket open');
    }

    function hdClose() {
      console.log('WebSocket close');
    }

    function hdError() {
      console.log('WebSocket error');
    }

    function hdConnection(ws) {
      console.log('WebSocket connected');
      ws.on('message', hdMessage)
    }

    function hdMessage(msg) {
      console.log(msg.toString());
      app.clients.forEach(c => {
        c.send(msg)
      })
    }

    // 验证名字不能相同 ???

    init()

  })(ws)